/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.buku;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class BukuAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward bukuList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        try {
            List listBuku = bukuService.loadAll();
            request.setAttribute(SISConstants.LIST_BUKU, listBuku);
        } catch (Exception ex) {
            Logger.getLogger(BukuAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.BUKU_LIST);
    }

    public ActionForward bukuAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        BukuForm bukuForm = (BukuForm) form;
        try {
            boolean kodeBukuIsUsed = bukuService.cekIsEmptyKode(bukuForm.getKodeBuku());
            if (kodeBukuIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.BUKU_LIST);
            } else {
                Buku buku = new Buku();
                buku.setKodeBuku(bukuForm.getKodeBuku());
                buku.setNamaBuku(bukuForm.getNamaBuku());
                buku.setPembuatBuku(bukuForm.getPembuatBuku());
                buku.setPenerbitBuku(bukuForm.getPenerbitBuku());
                buku.setTahunBuku(bukuForm.getTahunBuku());
                buku.setFlag(SISConstants.NOL);
                bukuService.saveBuku(buku);
                bukuForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(BukuAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.BUKU);
    }

    public ActionForward bukuEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        BukuForm bukuForm = (BukuForm) form;
        try {
            Buku buku = bukuService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            bukuForm.setKodeBuku(buku.getKodeBuku());
            bukuForm.setNamaBuku(buku.getNamaBuku());
            bukuForm.setPembuatBuku(buku.getPembuatBuku());
            bukuForm.setPenerbitBuku(buku.getPenerbitBuku());
            bukuForm.setTahunBuku(buku.getTahunBuku());
        } catch (Exception ex) {
            Logger.getLogger(BukuAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.BUKU_EDIT);
    }

    public ActionForward bukuUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        BukuForm bukuForm = (BukuForm) form;
        try {
            boolean kodeBukuIsUsed = bukuService.cekIsEmptyKode(bukuForm.getKodeBuku());
            if (kodeBukuIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.BUKU_EDIT);
            } else {
                Buku buku = bukuService.loadById(bukuForm.getId());
                buku.setKodeBuku(bukuForm.getKodeBuku());
                buku.setNamaBuku(bukuForm.getNamaBuku());
                buku.setPembuatBuku(bukuForm.getPembuatBuku());
                buku.setPenerbitBuku(bukuForm.getPenerbitBuku());
                buku.setTahunBuku(bukuForm.getTahunBuku());
                buku.setFlag(SISConstants.NOL);
                bukuService.updateBuku(buku);
                bukuForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(BukuAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.BUKU);
    }

    public ActionForward bukuDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        BukuForm bukuForm = (BukuForm) form;
        try {
            Buku buku = bukuService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            bukuService.deleteBuku(buku);
        } catch (Exception ex) {
            Logger.getLogger(BukuAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.BUKU);
    }
}